//
//  FFTextField.m
//  JomooCloudApp
//
//  Created by chewyong on 2018/4/12.
//  Copyright © 2018年 isoftstone. All rights reserved.
//

#import "FFTextField.h"

@implementation FFTextField

/**
 * 初始化文本框视图
 * @param frame 视图的frame
 * @return 返回初始化后的文本框对象
 */
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setUpUI];
    }
    return self;
}

/**
 * 通过xib创建时的初始化方法
 */
- (void)awakeFromNib {
    [super awakeFromNib];
    [self setUpUI];
}

/**
 * 设置UI界面
 */
- (void)setUpUI {
    //设置边框样式
    //self.layer.masksToBounds = YES;
    //self.layer.cornerRadius = 22;
    //self.backgroundColor = Default_FontColor;
    //self.layer.borderColor = [UIColor blackColor].CGColor;
    //self.layer.borderWidth = 1;

    //设置字体样式
    //self.font = [UIFont systemFontOfSize:15];

    //设置字体颜色
    //self.textColor = Default_FontColor;

    //设置光标颜色
    //self.tintColor= self.textColor;

    //设置占位符的颜色和大小
    //[self setValue:ZYRGBColor(167, 167, 167) forKeyPath:@"_placeholderLabel.textColor"];
    //[self setValue:[UIFont boldSystemFontOfSize:15] forKeyPath:@"_placeholderLabel.font"];

    //不成为第一响应者
    [self resignFirstResponder];
}

/**
 * 当前文本框聚焦时就会调用
 * @return 返回BOOL值，表示是否成为第一响应者
 */
- (BOOL)becomeFirstResponder {
    //修改占位文字颜色
    //[self setValue:self.textColor forKeyPath:@"_placeholderLabel.textColor"];
    return [super becomeFirstResponder];
}

/**
 * 绘制占位符
 * @param rect 占位符绘制的区域
 */
- (void)drawPlaceholderInRect:(CGRect)rect {
    UIColor *hintColor = [UIColor colorWithRed:167.0 / 255.0 green:167.0 / 255.0 blue:167.0 / 255.0 alpha:1.0];
    //计算占位文字的 Size
    CGSize placeholderSize = [self.placeholder sizeWithAttributes:@{NSFontAttributeName : self.font}];
    [self.placeholder drawInRect:CGRectMake(0, (rect.size.height - placeholderSize.height) / 2, rect.size.width, rect.size.height) withAttributes:@{NSForegroundColorAttributeName : hintColor, NSFontAttributeName : self.font}];
}

/**
 * 当前文本框失去焦点时就会调用
 * @return 返回BOOL值，表示是否放弃第一响应者
 */
- (BOOL)resignFirstResponder {
    //修改占位文字颜色
    //[self setValue:self.textColor forKeyPath:@"_placeholderLabel.textColor"];
    return [super resignFirstResponder];
}

//控制placeHolder的位置
//-(CGRect)placeholderRectForBounds:(CGRect)bounds {
//    CGRect inset = CGRectMake(bounds.origin.x+15, bounds.origin.y, bounds.size.width -15, bounds.size.height);
//    return inset;
//}

//控制显示文本的位置
//-(CGRect)textRectForBounds:(CGRect)bounds {
//    CGRect inset = CGRectMake(bounds.origin.x+15, bounds.origin.y, bounds.size.width -15, bounds.size.height);
//    return inset;
//}

//控制编辑文本的位置
//-(CGRect)editingRectForBounds:(CGRect)bounds {
//    CGRect inset = CGRectMake(bounds.origin.x +15, bounds.origin.y, bounds.size.width -15, bounds.size.height);
//    return inset;
//}

@end
